package com.rw.admin.controller;

import com.rw.common.pojo.Menu;
import com.rw.admin.service.ConfigInfoService;
import com.rw.admin.service.MenuService;
import com.rw.admin.utils.TreeUtlis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author rabit
 */
@Controller
public class ViewController {


    @Autowired
    private MenuService menuService;

    @Autowired
    private ConfigInfoService configInfoService;


    @RequestMapping("{url}")
    public String getView(@PathVariable String url){
        System.out.println("ViewController.getView执行");
        return url;
    }
    @RequestMapping("/")
    public String getIndex(HttpServletRequest request){
        return "index";
    }


    @RequestMapping("getMenu")
    @ResponseBody
    public  Map<String, Object> getMenu(){
        //这个数据应该是从数据库中读取出来的
        Map<String, Object> map =new HashMap<>();
        Map<String, Object> homeInfo =new HashMap<>();
        homeInfo.put("title","首页");
        homeInfo.put("href","page/welcome-1.html?t=1");
        map.put("homeInfo",homeInfo);
        Map<String, Object> logoInfo =new HashMap<>();
        String titleName = configInfoService.getValueByKey("web_title_name");
        if (titleName == null){
            titleName = "管理系统";
        }
        logoInfo.put("title",titleName);
        logoInfo.put("image","images/logo.png");
        logoInfo.put("href","");
        map.put("logoInfo",logoInfo);
        List<Menu> allMenus = TreeUtlis.buildTree(menuService.getAllMenus());
        map.put("menuInfo",allMenus);
        return map;
    }}
